#!/bin/bash
set -eux
scripts_dir=$(
    cd $(dirname "${BASH_SOURCE[0]}")
    pwd
)
project_dir=$(dirname "$scripts_dir")
# 检查protoc版本及protoc-gen-go是否存在
protoc_version=$(protoc --version | awk '{print $2}')
if [ "${protoc_version:0:2}" != "3." ]; then
    echo "protoc version must be 3.0+"
    exit 1
fi
command -v protoc-gen-go

mkdir -p /tmp/protogen/hundsun.com/hsl
rm -f /tmp/protogen/hundsun.com/hsl/hschain
ln -s -f $project_dir /tmp/protogen/hundsun.com/hsl/hschain

protos_dir=/tmp/protogen/hundsun.com/hsl/hschain/protos

read -p "please input the a relative path from protos dir: "

if [ -d "$project_dir/protos/$REPLY" ]; then
    for protos in $(find "$protos_dir/$REPLY" -name '*.proto' -exec dirname {} \; | sort | uniq); do
        protoc --proto_path=$protos_dir --go_out=plugins=grpc:/tmp/protogen $protos/*.proto
    done
elif [ -f "$project_dir/protos/$REPLY" ]; then
    protoc --proto_path=$protos_dir --go_out=plugins=grpc:/tmp/protogen $protos_dir/$REPLY
else
    echo "$project_dir/$REPLY not exists!"
fi
